﻿using System;
using System.Web;
using System.Data;
using AliCMS.SystemModule;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace AliCMS.Lib
{
    public class AdminPageBase : Page
    {
        public AdminPageBase(string pathInTemplate)
            : base(pathInTemplate)
        {
        }

        protected override void OnLoad(EventArgs e)
        {
            #region 没有登录，则返回登录页面
            if (Manager.User == null || !Manager.User.ContainsKey("Namespace") || Manager.User["Namespace"] != Channel[Alias.Module.Namespace].ToString())
                Response.Redirect(BuildUrl(null, "login", Request.Url.Query));
            #endregion

            #region 没有权限，则返回登录页面
            if (!Convert.IsDBNull(Channel[Alias.Channel.AdminRole]))
            {
                bool hasRight = false;
                string adminRole = Channel[Alias.Channel.AdminRole].ToString();
                string[] userRoles = Manager.User["Roles"].ToString().Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string userRole in userRoles)
                {
                    if (adminRole.IndexOf(userRole) != -1)
                    {
                        hasRight = true;
                        break;
                    }
                }

                if (!hasRight)
                    Response.Redirect(BuildUrl(null, "logout", null));
            }
            #endregion
        }

        #region Properties
        protected virtual string ContentLang
        {
            get
            {
                if (String.IsNullOrEmpty(_contentLang))
                {
                    if (String.IsNullOrEmpty(Request.QueryString["Lang"]))
                    {
                        HttpCookie cookie = Request.Cookies["DefaultContentLanguage"];
                        if (cookie == null)
                            _contentLang = Channel[Alias.Channel.Lang].ToString();
                        else
                            _contentLang = cookie.Value;
                    }
                    else
                    {
                        _contentLang = Request.QueryString["Lang"];
                    }
                }

                return _contentLang;
            }
        }
        protected string CurrentLanguage
        {
            get
            {
                DataRow[] rows = Manager.Languages.Select(Alias.Language.Ab + "='" + ContentLang + "'");
                return rows[0][Alias.Language.Name + PageLang].ToString();
            }
        }
        protected int PageSizeValue
        {
            get
            {
                if (String.IsNullOrEmpty(Request.QueryString["PageSize"]))
                    return 20;
                else
                    return Convert.ToInt32(Request.QueryString["PageSize"]);
            }
        }
        protected string ModuleJsPath
        {
            get
            {
                int pageIndex = PathInTemplate.IndexOf("/page/");
                if (pageIndex == -1)
                    return String.Empty;

                return PathInTemplate.Substring(0, pageIndex + 1) + "js/";
            }
        }
        #endregion

        #region Fields
        private string _contentLang;
        #endregion
    }
}
